package com.swordget.service.impl;

import com.swordget.dao.HandlerLogDao;
import com.swordget.service.HandlerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: David
 * @Date: Created in 17:29 2020/4/17
 * @Description: 日志服务具体实现
 * @Modified By:
 * @Version:
 */
@Service
public class HandlerLogServiceImp implements HandlerLogService {

    private HandlerLogDao handlerLogDao;

    @Autowired
    public HandlerLogServiceImp(HandlerLogDao handlerLogDao) {
        this.handlerLogDao = handlerLogDao;
    }

    @Override
    public String addUploadLog(File file, String loginId) {
        String serialNo = System.currentTimeMillis() + String.format("%06d", Math.round(Math.random() * 1000000));
        String fileName = file.getName();
        String filePath = file.getAbsolutePath();
        filePath = filePath.replaceAll("\\\\", "/");
        filePath = filePath.replaceAll("\\\\\\\\", "/");
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String createTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
        return handlerLogDao.addUploadLog(serialNo, fileName, filePath, loginId, createDate, createTime);
    }

    @Override
    public String findUploadFilePath(String fileId) {
        return handlerLogDao.findUploadFilePath(fileId);
    }
}
